// Copyright 2022 a.o.smith ALL Rights Reserved
package com.liuxinlong.modules.moc.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.liuxinlong.annotation.LogAnnotation;
import com.liuxinlong.constant.LogBusinessConstants;
import com.liuxinlong.constant.LogOperateConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartRequest;

import com.liuxinlong.common.ImportResp;
import com.liuxinlong.common.UploadService;
import com.liuxinlong.modules.common.controller.request.BaseDeleteReq;
import com.liuxinlong.modules.common.controller.request.QueryBaseReq;
import com.liuxinlong.modules.common.controller.response.BaseResp;
import com.liuxinlong.modules.entity.MocDetail;
import com.liuxinlong.modules.moc.controller.request.ImportMocDetailReq;
import com.liuxinlong.modules.moc.controller.request.UpdateMocDetailReq;
import com.liuxinlong.modules.moc.service.MocDetailService;

/**
 * 变更记录详情详情控制层
 *
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2022年1月21日
 */
@RestController
@RequestMapping("/moc/detail")
public class MocDetailController {
	@Autowired
	private MocDetailService mocDetailService;

	@Autowired
	private UploadService uploadService;

	/**
	 * 分页查询物料变更记录详情信息
	 * 
	 * @param req 请求信息
	 * @return 物料变更记录详情信息列表
	 */
	@PostMapping("page")
//	@LogAnnotation(value ="分页查询",business = LogBusinessConstants.MOC_DETAIL,operateType = LogOperateConstants.OPERATE_QUERY)
	public BaseResp pageDetailInfo(@RequestBody QueryBaseReq req) {
		Map<String, Object> queryMap = new HashMap<>();
		queryMap.put("startNum", req.getStartNum());
		queryMap.put("pageSize", req.getPageSize());
		queryMap.put("recordId", req.getId());
		List<Map<String, Object>> dataList = mocDetailService.pageDetailInfo(queryMap);
		int totalNum = mocDetailService.getDetailCount(queryMap);
		BaseResp resp = new BaseResp();
		resp.setRetMessage("分页查询物料变更记录详情信息成功");
		resp.setDataList(dataList);
		resp.setTotalNum(totalNum);
		return resp;
	}

	/**
	 * 新增物料变更记录详情信息
	 * 
	 * @param req 请求信息
	 * @return 成功标识
	 */
	@PostMapping("add")
	@LogAnnotation(value ="新增物料变更记录详情",business = LogBusinessConstants.MOC_DETAIL,operateType = LogOperateConstants.OPERATE_ADD)
	public BaseResp addDetailInfo(@RequestBody UpdateMocDetailReq req) {
		MocDetail moc = new MocDetail();
		moc.setRecordId(req.getRecordId());
		moc.setMaterial(req.getMaterial());
		moc.setDescription(req.getDescription());
		moc.setRegion(req.getRegion());
		moc.setParam(req.getParam());
		moc.setOldValue(req.getOldValue());
		moc.setNewValue(req.getNewValue());
		moc.setRemark(req.getRemark());
		mocDetailService.addDetailInfo(moc);
		BaseResp resp = new BaseResp();
		resp.setRetMessage("新增物料变更记录详情信息成功");
		return resp;

	}

	/**
	 * 修改物料变更记录详情信息
	 * 
	 * @param req 请求信息
	 * @return 成功标识
	 */
	@PostMapping("update")
	@LogAnnotation(value ="修改物料变更记录详情",business = LogBusinessConstants.MOC_DETAIL,operateType = LogOperateConstants.OPERATE_MODIFY)
	public BaseResp updateDetailInfo(@RequestBody UpdateMocDetailReq req) {
		MocDetail moc = new MocDetail();
		moc.setId(req.getId());
		moc.setRecordId(req.getRecordId());
		moc.setMaterial(req.getMaterial());
		moc.setDescription(req.getDescription());
		moc.setParam(req.getParam());
		moc.setOldValue(req.getOldValue());
		moc.setNewValue(req.getNewValue());
		moc.setRegion(req.getRegion());
		moc.setRemark(req.getRemark());
		mocDetailService.updateDetailInfo(moc);
		BaseResp resp = new BaseResp();
		resp.setRetMessage("修改物料变更记录详情信息成功");
		return resp;
	}

	/**
	 * 删除物料变更记录详情信息
	 * 
	 * @param req 请求信息
	 * @return 成功标识
	 */
	@PostMapping("delete")
	@LogAnnotation(value ="删除物料变更记录详情",business = LogBusinessConstants.MOC_DETAIL,operateType = LogOperateConstants.OPERATE_DELETE)
	public BaseResp deleteDetailInfo(@RequestBody BaseDeleteReq req) {
		mocDetailService.deleteDetailInfo(req.getId());
		BaseResp resp = new BaseResp();
		resp.setRetMessage("删除物料变更记录详情信息成功");
		return resp;
	}

	/**
	 * 导入数据预处理
	 *
	 * @param multipartRequest 文件
	 * @return 数据
	 */
	@PostMapping("import")
	@LogAnnotation(value ="导入物料变更记录详情数据",business = LogBusinessConstants.MOC_DETAIL,operateType = LogOperateConstants.OPERATE_IMPORT)
	public ImportResp importDetails(MultipartRequest multipartRequest) {
		Map<String, Object> map = uploadService.uploadFileLocal(multipartRequest);
		ImportResp resp = mocDetailService.importDetails((String) map.get("fileRealPath"));
		return resp;
	}

	/**
	 * 导入变更记录详情信息
	 *
	 * @param req 请求体
	 * @return 返回导入失败数据
	 */
	@PostMapping("batch-add")
	@LogAnnotation(value ="导入物料变更记录详情数据",business = LogBusinessConstants.MOC_DETAIL,operateType = LogOperateConstants.OPERATE_IMPORT)
	public ImportResp batchAddDetail(@RequestBody ImportMocDetailReq req) {
		return mocDetailService.batchAddDetail(req.getDetailList(), req.getRecordId());
	}
}
